package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.DrInterfaceBo;
import com.ruoyi.tb.lmt.domain.vo.DrInterfaceVo;
import com.ruoyi.tb.lmt.domain.DrInterface;
import com.ruoyi.tb.lmt.mapper.DrInterfaceMapper;
import com.ruoyi.tb.lmt.service.IDrInterfaceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 油压减震器Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class DrInterfaceServiceImpl implements IDrInterfaceService {

    private final DrInterfaceMapper baseMapper;

    /**
     * 查询油压减震器
     */
    @Override
    public DrInterfaceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询油压减震器列表
     */
    @Override
    public TableDataInfo<DrInterfaceVo> queryPageList(DrInterfaceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrInterface> lqw = buildQueryWrapper(bo);
        Page<DrInterfaceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询油压减震器列表
     */
    @Override
    public List<DrInterfaceVo> queryList(DrInterfaceBo bo) {
        LambdaQueryWrapper<DrInterface> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrInterface> buildQueryWrapper(DrInterfaceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrInterface> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, DrInterface::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), DrInterface::getDjNo, bo.getDjNo());
        lqw.eq(bo.getDrId() != null, DrInterface::getDrId, bo.getDrId());
        lqw.eq(StringUtils.isNotBlank(bo.getDrDispCorrect()), DrInterface::getDrDispCorrect, bo.getDrDispCorrect());
        lqw.eq(StringUtils.isNotBlank(bo.getDrDispZero()), DrInterface::getDrDispZero, bo.getDrDispZero());
        lqw.eq(StringUtils.isNotBlank(bo.getDrForceCorrect1()), DrInterface::getDrForceCorrect1, bo.getDrForceCorrect1());
        lqw.eq(StringUtils.isNotBlank(bo.getDrForceZero1()), DrInterface::getDrForceZero1, bo.getDrForceZero1());
        lqw.eq(StringUtils.isNotBlank(bo.getDrForceCorrect2()), DrInterface::getDrForceCorrect2, bo.getDrForceCorrect2());
        lqw.eq(StringUtils.isNotBlank(bo.getDrForceZero2()), DrInterface::getDrForceZero2, bo.getDrForceZero2());
        lqw.eq(StringUtils.isNotBlank(bo.getDrAntitude()), DrInterface::getDrAntitude, bo.getDrAntitude());
        lqw.eq(StringUtils.isNotBlank(bo.getDrMonoDamperType()), DrInterface::getDrMonoDamperType, bo.getDrMonoDamperType());
        lqw.eq(StringUtils.isNotBlank(bo.getDrMultiDamperType()), DrInterface::getDrMultiDamperType, bo.getDrMultiDamperType());
        lqw.eq(StringUtils.isNotBlank(bo.getDrXScale()), DrInterface::getDrXScale, bo.getDrXScale());
        lqw.eq(StringUtils.isNotBlank(bo.getDrYScale()), DrInterface::getDrYScale, bo.getDrYScale());
        lqw.eq(StringUtils.isNotBlank(bo.getDrTimeFactor()), DrInterface::getDrTimeFactor, bo.getDrTimeFactor());
        lqw.eq(StringUtils.isNotBlank(bo.getDrCommPort()), DrInterface::getDrCommPort, bo.getDrCommPort());
        lqw.eq(StringUtils.isNotBlank(bo.getDrVelocityInterval()), DrInterface::getDrVelocityInterval, bo.getDrVelocityInterval());
        lqw.eq(StringUtils.isNotBlank(bo.getDrOperator()), DrInterface::getDrOperator, bo.getDrOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getDrChecker()), DrInterface::getDrChecker, bo.getDrChecker());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryDamperType()), DrInterface::getDrQueryDamperType, bo.getDrQueryDamperType());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryCarNo()), DrInterface::getDrQueryCarNo, bo.getDrQueryCarNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryCarType()), DrInterface::getDrQueryCarType, bo.getDrQueryCarType());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryDateYear()), DrInterface::getDrQueryDateYear, bo.getDrQueryDateYear());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryDateMonth()), DrInterface::getDrQueryDateMonth, bo.getDrQueryDateMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryDateDay()), DrInterface::getDrQueryDateDay, bo.getDrQueryDateDay());
        lqw.eq(StringUtils.isNotBlank(bo.getDrAutoForceZero()), DrInterface::getDrAutoForceZero, bo.getDrAutoForceZero());
        lqw.eq(StringUtils.isNotBlank(bo.getDrFSlineMark()), DrInterface::getDrFSlineMark, bo.getDrFSlineMark());
        lqw.eq(StringUtils.isNotBlank(bo.getDrFVlineMark()), DrInterface::getDrFVlineMark, bo.getDrFVlineMark());
        lqw.eq(StringUtils.isNotBlank(bo.getDrMultifVlineMark()), DrInterface::getDrMultifVlineMark, bo.getDrMultifVlineMark());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryDamperTypeEnable()), DrInterface::getDrQueryDamperTypeEnable, bo.getDrQueryDamperTypeEnable());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryCarNoEnbale()), DrInterface::getDrQueryCarNoEnbale, bo.getDrQueryCarNoEnbale());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryCarTypeEnable()), DrInterface::getDrQueryCarTypeEnable, bo.getDrQueryCarTypeEnable());
        lqw.eq(StringUtils.isNotBlank(bo.getDrQueryDateEnable()), DrInterface::getDrQueryDateEnable, bo.getDrQueryDateEnable());
        lqw.eq(StringUtils.isNotBlank(bo.getDrBlackPrint()), DrInterface::getDrBlackPrint, bo.getDrBlackPrint());
        lqw.eq(StringUtils.isNotBlank(bo.getDrColorPrint()), DrInterface::getDrColorPrint, bo.getDrColorPrint());
        lqw.eq(StringUtils.isNotBlank(bo.getDrDataNum()), DrInterface::getDrDataNum, bo.getDrDataNum());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), DrInterface::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), DrInterface::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, DrInterface::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), DrInterface::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, DrInterface::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增油压减震器
     */
    @Override
    public Boolean insertByBo(DrInterfaceBo bo) {
        DrInterface add = BeanUtil.toBean(bo, DrInterface.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改油压减震器
     */
    @Override
    public Boolean updateByBo(DrInterfaceBo bo) {
        DrInterface update = BeanUtil.toBean(bo, DrInterface.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DrInterface entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除油压减震器
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Long getMaxDrID() {

        return baseMapper.getMaxDrID();
    }
}
